/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.camachosanchez.controler.jdbc;

import com.camachosanchez.bean.TiposEje;
import com.camachosanchez.util.ConexionSQL;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Zully
 */
public class TipoEjeJdbcControler {
    
    public static boolean cantidadDisponibleTipoEje() {
        Connection cn = ConexionSQL.obtenerConexion();
        try {
            Statement st = cn.createStatement();
            ResultSet rs = st.executeQuery("SELECT * FROM tipos_eje WHERE estado = 1 AND activo = 1 LIMIT 1");
            if (rs.first()) {
                return true;
            }
            rs.close();
            st.close();
            cn.close();
        } catch (Exception e) {
            Logger.getLogger(NeumaticosJdbcControler.class.getName()).log(Level.SEVERE, null, e);
        }
        return false;
    }
    
    public int existeTipoEje(String nombre) {
        Connection cn = ConexionSQL.obtenerConexion();
        int id = 0;
        try {
            Statement st = cn.createStatement();
            ResultSet rs = st.executeQuery("SELECT id FROM tipos_eje where nombre='" + nombre + "' and estado = 1");
            if (rs.next()) {
                id = rs.getInt(1);
            }
            rs.close();
            st.close();
            cn.close();
        } catch (Exception e) {
            Logger.getLogger(NeumaticosJdbcControler.class.getName()).log(Level.SEVERE, null, e);
        }
        return id;
    }
    
    public List<TiposEje> findTipoEjeFromTipoVehiculo(int idTipoVehiculo) {
        List<TiposEje> lst = new ArrayList<TiposEje>();
        Connection cn = ConexionSQL.obtenerConexion();
        Statement st;
        try {
            st = cn.createStatement();
            ResultSet rs = st.executeQuery("SELECT TE.id, TE.nombre, COUNT(E.posicion) AS ejes, SUM(E.num_neumaticos) AS num_neumaticos,"
                                        + " TE.estado, TE.activo"
                                        + " FROM tipos_eje TE LEFT JOIN ejes E ON E.id_eje = TE.id"
                                        + " WHERE TE.id_tipo_vehiculo=" + idTipoVehiculo
                                        + " GROUP BY TE.id ORDER BY TE.nombre;");
            while (rs.next()) {
                TiposEje tipoE = new TiposEje();
                tipoE.setId(rs.getInt(1));
                tipoE.setNombre(rs.getString(2));
//                tipoE.setEjes(rs.getInt(3));
//                tipoE.setNum_neumaticos(rs.getInt(4));
                tipoE.setEstado(rs.getBoolean(5));
                tipoE.setActivo(rs.getBoolean(6));
                lst.add(tipoE);
            }
            rs.close();
            st.close();
            cn.close();
        } catch (SQLException ex) {
            Logger.getLogger(TalleresJdbcControler.class.getName()).log(Level.SEVERE, null, ex);
        }
        return lst;
    }
    
    public byte eliminarTipoEje(int id) {
        byte estado = 0;
        try {
            Connection cn = ConexionSQL.obtenerConexion();
            CallableStatement sp = cn.prepareCall("{ call eliminar_tipo_eje(?) }");
            sp.setInt("idTipoEje", id);
            ResultSet rs = sp.executeQuery();
            if(rs.next()){
                estado = rs.getByte(1);
            }
            rs.close();
            sp.close();
            cn.close();
        } catch (SQLException ex) {
            Logger.getLogger(TipoEjeJdbcControler.class.getName()).log(Level.SEVERE, null, ex);
        }
        return estado;
    }
}
